import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;

import java.util.ArrayList;
import java.util.List;

public class GeometryUtil {
    public static GeometryFactory factory = new GeometryFactory();

    /**
     * 获取 List<Geometry>
     */
    public static List<Geometry> getList(Geometry geometry) {
        List<Geometry> list = new ArrayList<>();
        if(geometry==null){
            return list;
        }

        // 几何类型
        String geometryType = geometry.getGeometryType();

        // Polygon || LineString
        if (geometryType.equals(Geometry.TYPENAME_POLYGON)
                || geometryType.equals(Geometry.TYPENAME_LINESTRING)) {
            list.add(geometry);
        }

        // GeometryCollection || MultiPolygon || MultiLineString
        if (geometryType.equals(Geometry.TYPENAME_GEOMETRYCOLLECTION)
                || geometryType.equals(Geometry.TYPENAME_MULTIPOLYGON)
                || geometryType.equals(Geometry.TYPENAME_MULTILINESTRING)) {
            for (int i = 0; i < geometry.getNumGeometries(); i++) {
                Geometry geometryN = geometry.getGeometryN(i);
                if (geometryN.getGeometryType().equals(Geometry.TYPENAME_POLYGON)
                        || geometryN.getGeometryType().equals(Geometry.TYPENAME_LINESTRING)) {
                    list.add(geometryN);
                }
            }
        }

        return list;
    }
}
